<?php 
session_start(); 
$GLOBALS['baseUrl'] = 'https://secure719.hostgator.com/~sixty06/privr';

$GLOBALS['localBaseUrl'] = '/home/	sixty06/privr';
?>
<?php include("php/sessionValidate.php"); ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Privr - Private Messaging for Everyone</title>
<link href="main.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="<?php echo $GLOBALS['baseUrl']?>/javascript/RSA.js"></script>
<script type="text/javascript" src="<?php echo $GLOBALS['baseUrl']?>/javascript/BigInt.js"></script>
<script type="text/javascript" src="<?php echo $GLOBALS['baseUrl']?>/javascript/Barrett.js"></script>
<script type="text/javascript">
	<!--
	var baseUrl = '<?php echo $GLOBALS['baseUrl']?>';
	var key;
	var groupKeys = new Object();
	var inviteUserKeys = new Object();
	
	function initialLoad() {
		loadKey();
		refresh();
	}
	
	function refresh() {
		ajaxShowMessages();
		ajaxShowGroupMembership();
		ajaxShowInvites();
		ajaxShowOtherMembers();
		ajaxShowMyInvites();
		
		var t=setTimeout("loadGroupKeys();",2000);
	}
	
	var userName =  '<?php echo $_SESSION['username'] ?>';
	var userPublicKey;
	var userPrivateKey;
	var userModulus;
	var key;
	
	function showKey( keyVar ) {
		alert('public: ' + keyVar.publicKey
			+ '\nprivate: ' + keyVar.privateKey
			+ '\nmod: ' + keyVar.modulus);
	}

		
	function loadKey() {
		setMaxDigits(131);
		
		userPublicKey=ajaxLoadUserPublicKey(userName);
		userModulus=ajaxLoadUserModulus(userName);		
		userPrivateKey = prompt('Enter your private key:','key'); //fixme: debug
		//userPrivateKey = '50b172bda540994fdcabd3800d469139f336b925ece08a5c889f39ac1f25e61c9ec1e9588fd493ecfcaf5dc7e03e5ef586ef496a64a1bd73c07a65381c64dd9b8054e993a4f38906600da71871d1fee9f59d4b1391de39fc1401395ab3090cc31cdf875502704e2fbab6f70470f84019a6c666f14dc241f0a62955792c706043';  //fixme:debug
	}

	
	function getRadioArray (groupName) {
		var radioArray = new Array();
		var nodeList = document.getElementsByTagName("input");
		for( var i = 0; i < nodeList.length; i++) {
			if( nodeList.item(i).name == groupName ) {
				radioArray[radioArray.length] = nodeList.item(i).value;
			}
		}
		return radioArray;
	}

	function loadGroupKeys() {
		setMaxDigits(131);
		
		var groupsArray = getRadioArray('groups');
		
		for ( var i = 0; i < groupsArray.length; i++ ) {
			//alert('loading group: ' + groupsArray[i]);	
			loadGroupKey( groupsArray[i] );
		}
		//alert( groupsArray.toSource() );
	}
	
	function loadGroupKey( groupName ) {
		setMaxDigits(131);
		
		var encryptedGroupPrivateKey = ajaxGetUserGroupKey(groupName);
		//alert('encryptedGroupPrivateKey = ' + encryptedGroupPrivateKey);
		
		//alert('before decrypt group key');
		var groupPrivateKey = decrypt( encryptedGroupPrivateKey );
		//alert('groupPrivateKey=' + groupPrivateKey);
		
		var groupPublicKey = ajaxLoadGroupPublicKey( groupName );
		//alert('groupPublicKey=' + groupPublicKey);
		
		var groupModulus = ajaxLoadGroupModulus( groupName );
		//alert('groupModulus=' + groupModulus);

		var groupKey = new RSAKeyPair( userPublicKey, userPrivateKey, userModulus );
		
		groupKeys[groupName] = groupKey;
	}

	function loadInviteUserKey( inviteUser ) {
	
		//alert('inviteUser=' + inviteUser );
		//alert(inviteUserKeys[inviteUser]);
		//if already cached, nothing to do
		if ( inviteUser in inviteUserKeys ) {
			return inviteUserKeys[inviteUser];
		}
		
		setMaxDigits(131);
		
		var inviteUserPublicKey = ajaxLoadUserPublicKey( inviteUser );
		//alert ('inviteUserPublicKey=' + inviteUserPublicKey);
		var inviteUserModulus = ajaxLoadUserModulus( inviteUser );
		//alert ('inviteUserModulus=' + inviteUserModulus);
		
		var inviteUserPrivateKey = '0'; //dummy value since only encrypting;
		
		var inviteUserKey = new RSAKeyPair( inviteUserPublicKey, inviteUserPrivateKey, inviteUserModulus );
		//alert('inviteUserKey');
		//showKey(inviteUserKey);
		
		inviteUserKeys[inviteUser] = inviteUserKey;
		//showKey( inviteUserKeys[inviteUser] );
		
		return inviteUserKey;
	}
	
	function testEncryptDecrypt( text ) {
		
		alert( 'text: ' + text );
		var encrypted = encrypt( text );
		alert( 'encrypted: ' + encrypted);
		var decrypted = decrypt( encrypted );
		alert( 'decrypted: ' + decrypted);
	}

	function testEncryptDecryptGroupPrivateKey( ) {
		
		var groupPrivateKey = 'c5f889337302a79d2dc4f95564be36c78ed6a16574b18cb31858f18b4cbc4fe43137b2dc30377c6538f040798c1aea6477fba31f4e6726dbc3718b14d292e7dec02040bb34fe0cd5b11e8673398c4244ed14abff3707fec0f44c8f41689893b693ad2c75e3737bdbd390c6bcf067f3bc57da40706b5d31ec16617a3fffda89';

		//1d5facde3a4f90771edd81113189474c9d7cd5807cee63d4c295dfa360a2721f22ad632641e58447d887dc1860a7ff1acc48e3f339128fa373544fff3ce6c732d9e4fa317da3179b555392f18868d5b5f4e7d164d33326fa896599dcc0812db09fe2807ace1d6926133780b3fe571c33ffee8016400c842c64bba9d0d408aabd b6274a2e2ded737024d059742a036145e13a3c1164cd14e2d1b8bc37adb23fcf0b716b2a259ac94cf5078c658d43385639194cd2decea2fc209e3c9affaf98474bcd67da4586e1247e6ceb894435428bc512db75780f13db2d0692b50fbf5a563dba2d3fbd0848b28edae0e33f73293d27446bc242e7a70e31e12ff13bca7f2a a659025dd93d867c6b807c68bac1a2634b994d05f052beb2415abfed42f440019916c1edbe3ef033488c8de93dffd57c77b47988baa79888927d872763c321751096ece15ca1e00373756490e1463412641323bdfafa01bb493d452e77b9d98176de587388c92f2a863be3bdd7b7f77e043f89059854bc211228a9fabaed0246
		//1d5facde3a4f90771edd81113189474c9d7cd5807cee63d4c295dfa360a2721f22ad632641e58447d887dc1860a7ff1acc48e3f339128fa373544fff3ce6c732d9e4fa317da3179b555392f18868d5b5f4e7d164d33326fa896599dcc0812db09fe2807ace1d6926133780b3fe571c33ffee8016400c842c64bba9d0d408aabd b6274a2e2ded737024d059742a036145e13a3c1164cd14e2d1b8bc37adb23fcf0b716b2a259ac94cf5078c658d43385639194cd2decea2fc209e3c9affaf98474bcd67da4586e1247e6ceb894435428bc512db75780f13db2d0692b50fbf5a563dba2d3fbd0848b28edae0e33f73293d27446bc242e7a70e31e12ff13bca7f2a a659025dd93d867c6b807c68bac1a2634b994d05f052beb2415abfed42f440019916c1edbe3ef033488c8de93dffd57c77b47988baa79888927d872763c321751096ece15ca1e00373756490e1463412641323bdfafa01bb493d452e77b9d98176de587388c92f2a863be3bdd7b7f77e043f89059854bc211228a9fabaed0246
		alert( 'groupPrivateKey: ' + groupPrivateKey );
		var encrypted = encrypt( groupPrivateKey );
		alert( 'encrypted: ' + encrypted);
		var decrypted = decrypt( encrypted );
		alert( 'decrypted: ' + decrypted);
	}

	
	function testCreateGroup() {
		createGroup();
		document.getElementById('groupName').value = 'group1';
		document.getElementById('groupPublicKey').value = '7753edb9acfde9c8594975e1262d85a57741b4442357bdd50ad2880dbb04a97ecb1ad3ade4ebd62b0ecd27d9cfbcf4712bf6a2693ae998db3c00c0aa9480d0630310e67b9ad1496fbbec58f8421ad06087cb6d159e6be093420aaa82915c08db77dca28bfba7e45144233a7633476743cf2b2a817cdb3fdf325870e349620a9';
		document.getElementById('groupPrivateKey').value = 'c5f889337302a79d2dc4f95564be36c78ed6a16574b18cb31858f18b4cbc4fe43137b2dc30377c6538f040798c1aea6477fba31f4e6726dbc3718b14d292e7dec02040bb34fe0cd5b11e8673398c4244ed14abff3707fec0f44c8f41689893b693ad2c75e3737bdbd390c6bcf067f3bc57da40706b5d31ec16617a3fffda89';
		document.getElementById('groupKeyModulus').value = '89bd9f24cc4e1a459ea37b59648804b50b37d87a23a803e3ef9db5cc2ee74f4ccd5fef7243252dce168f145165f34eb63c26d3e7b4e5358de892e90e3811c1878d7ec0d964b475942b96e9c97f5b396a3d60c89e09390b3b8bcf579a4ba011b60a3c5a62328f98fb4ce799c554abc1a554863fc8da269388beaa5da03edd30b';
		document.getElementById('groupDescription').value = 'desc1';
		ajaxCreateGroup();
	}
	
	function testSendMessage() {
		sendGroupMessage();
		
		setCheckedValue('groups', 'group1');
		document.getElementById('message').value = 'test message';
		document.getElementById('retentionMode').value = '1';
		document.getElementById('expirationDate').value = '2011-05-05';
		
		ajaxSendGroupMessage();
		
	}

	function encrypt( text ) {
		setMaxDigits(131);
		//alert('upubk: ' + userPublicKey );
		//alert('uprivk: ' + userPrivateKey );
		//alert('umod: ' + userModulus );
		key = new RSAKeyPair( userPublicKey, userPrivateKey, userModulus );		
		
		return encryptedString( key, text )
	}
	
	function decrypt( text ) {
		/*
		alert('userPublicKey=' + userPublicKey 
			+ ',<br/> ' + 'userPrivateKey=' + userPrivateKey
			+ ',<br/> ' + 'userModulus=' + userModulus
			+ ',<br/> ' + 'text=' + text);
		*/
		key = new RSAKeyPair( userPublicKey, userPrivateKey, userModulus );
		
		return decryptedString( key, text )
	}
	
	function encryptGroupMessage(groupName, message) {
	
		var groupKey = groupKeys[groupName];
		//alert('message=' + message);
		var retVal = encryptedString(groupKey, message);
		//alert('encrypted message =' + retVal);
		return retVal
	}
	
	function encryptInviteMessage( inviteUser, message) {
	
		//alert('inviteUser=' + inviteUser + ', message=' + message);
		var userKey = loadInviteUserKey( inviteUser );
		//alert('message=' + message);
		var retVal = encryptedString( userKey, message );
		//alert('encrypted message =' + retVal);
		return retVal
	}	
	
	function decryptGroupMessage(groupName, message) {
		
			
		var encryptedGroupPrivateKey = ajaxGetUserGroupKey(groupName);
		var groupPrivateKey = decrypt(encryptedGroupPrivateKey);
		var groupKey = new RSAKeyPair( userPublicKey, userPrivateKey, userModulus );

		return decryptedString(groupKey, message);
	}

	
	//loads the user's public key into global userPublicKey variable
	function ajaxLoadUserPublicKey(userName) {
		var retValue;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				retValue=xmlhttpInv.responseText;
				//userPublicKey = retValue;
				//alert('in: ' + userPublicKey);
			}	
		}
		//note: synchronous request from 3rd parameter = false
		xmlhttpInv.open("GET", baseUrl + "/php/getUserPublicKey.php?userName=" + userName ,false);
		xmlhttpInv.send();
		//alert('out: ' + userPublicKey);
		return retValue;
	}

	function ajaxLoadGroupModulus(groupName) {
		
		var retValue;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				retValue=xmlhttpInv.responseText;
			}	
		}
		//note: synchronous request from 3rd parameter = false
		xmlhttpInv.open("GET", baseUrl + "/php/getGroupModulus.php?groupName=" + groupName ,false);
		xmlhttpInv.send();
		return retValue;
	}
	
	function ajaxLoadGroupPublicKey( groupName ) {
		
		var retValue;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				retValue=xmlhttpInv.responseText;
			}	
		}
		//note: synchronous request from 3rd parameter = false
		xmlhttpInv.open("GET", baseUrl + "/php/getGroupPublicKey.php?groupName=" + groupName ,false);
		xmlhttpInv.send();
		return retValue;
	}
	
	function ajaxLoadUserModulus(userName) {
		
		var retValue;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				retValue=xmlhttpInv.responseText;
				//userModulus = retValue;
			}	
		}
		//note: synchronous request from 3rd parameter = false
		xmlhttpInv.open("GET", baseUrl + "/php/getUserModulus.php?userName=" + userName ,false);
		xmlhttpInv.send();
		return retValue;
	}
	
	//TODO: could cache this in an associate array, especially after decryption
	function ajaxGetUserGroupKey(groupName) {
		var userName = '<?php echo $_SESSION['username'] ?>';
		
		var retValue;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				retValue=xmlhttpInv.responseText;
			}	
		}
		//note sync	
		xmlhttpInv.open("GET", baseUrl + "/php/getUserGroupKey.php?userName=" + userName + "&groupName=" + groupName ,false);
		xmlhttpInv.send();
		
		return retValue;
	}
	
	
	function hideSideBarItems() {
		
		document.getElementById("inviteUserDiv").style.display='none';
		document.getElementById("showMessagesDiv").style.display='none';
		document.getElementById("showMessagesGroup2").style.display='none'; //TODO: deprecate
		document.getElementById("sendMessageDiv").style.display='none';
		document.getElementById("showUsersDiv").style.display='none';
		document.getElementById("newGroupDiv").style.display='none';
		document.getElementById("deleteGroupDiv").style.display='none';
		
		document.getElementById("groupMembers").style.display='none';
		
		document.getElementById("messages").style.display='none';
		document.getElementById("newMessageForm").style.display='none';
		
		document.getElementById("logoutDiv").style.display='none';
		
		
		//new group
		

	}
	
	function logout() {
		hideSideBarItems();
		document.getElementById("logoutDiv").style.display='block';
		window.location=baseUrl + '/logout.php';
		//ajaxLogout();
	}

	function createGroup() {
		hideSideBarItems();
		document.getElementById("newGroupDiv").style.display='block';
	
		//if (document.getElementById('newGroup').innerHTML != ""){
		//	document.getElementById('newGroup').innerHTML = "";
		//} else {
		document.getElementById('newGroup').innerHTML = 
			"<table>\n\r"
			+ "<tr><td>group name:</td> <td><input type='text' id='groupName' name='groupName' /></td></tr>\n\r"
			+ "<tr><td>group public key:</td> <td><input type='text' id='groupPublicKey' name='groupPublicKey' /></td></tr>\n\r"
			+ "<tr><td>group private key (will be encrypted):</td> <td><input type='text' id='groupPrivateKey' name='groupPrivateKey' /></td></tr>\n\r"
			+ "<tr><td>group key modulus:</td> <td><input type='text' id='groupKeyModulus' name='groupKeyModulus' /></td></tr>\n\r"
			+ "<tr><td>group description:</td> <td><input type='text' id='groupDescription' name='groupDescription' /></td></tr>\n\r"
			+ "</table>"
			+ "<button onclick='ajaxCreateGroup()' >Create Group</button>";	
		//}		
	}
	
	function sendGroupMessage() {
		hideSideBarItems();
		
		var groupName = getRadioValue('groups');
		var userName = '<?php echo $_SESSION['username'] ?>';
		
		document.getElementById('sendMessageDiv').style.display='block';
		document.getElementById('sendMessage').innerHTML = 
			"<table>\n\r"
			+ "<tr><td>group name:</td> <td>" + groupName + "</td></tr>\n\r"
			+ "<tr><td>user name:</td> <td>" + userName + "</td></tr>\n\r"
			+ "<tr><td>message:</td> <td><input type='text' id='message' name='message' /></td></tr>\n\r"
			+ "<tr><td>retention mode:</td> <td><input type='text' id='retentionMode' name='retentionMode' /></td></tr>\n\r"
			+ "<tr><td>expiration date:</td> <td><input type='text' id='expirationDate' name='expirationDate' /></td></tr>\n\r"
			+ "</table>"
			+ "<button onclick='ajaxSendGroupMessage()' >Send Message to Group</button>";
	}
	
	function showGroupMessages() {
		hideSideBarItems();
		
		document.getElementById('showMessagesDiv').style.display='block';
		ajaxShowGroupMessages();		
	}
	
	function showGroupUsers() {
		hideSideBarItems();
		document.getElementById('showUsersDiv').style.display='block';
		ajaxShowGroupUsers();		
	}
	
	function setCheckedValue(radioId, newValue) {
		var radioObj = document.getElementById(radioId);
		if(!radioObj)
			return;
		var radioLength = radioObj.length;
		if(radioLength == undefined) {
			radioObj.checked = (radioObj.value == newValue.toString());
			return;
		}
		for(var i = 0; i < radioLength; i++) {
			radioObj[i].checked = false;
			if(radioObj[i].value == newValue.toString()) {
				radioObj[i].checked = true;
			}
		}
	}

	function getRadioValue(idOrName) {
		var radios = document.getElementsByTagName('input');
		for (var i=0; i<radios.length; i++) {
			var input = radios[ i ];    
			if (input.type == 'radio' && input.name == idOrName && input.checked) { 
				
				value = input.value;
				break;
			}
		}
		return value;
	}

	function ajaxLogout() {
		document.getElementById("logoutInfo").innerHTML='temp1';

		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				document.getElementById("logoutInfo").innerHTML='temp2';
				document.getElementById("logoutInfo").innerHTML=xmlhttpInv.responseText;
			}	
		}
		xmlhttpInv.open("GET", baseUrl + "/logout.php" ,true);
		xmlhttpInv.send();
	}
	
	function ajaxShowGroupMessages() {
		var userName = '<?php echo $_SESSION['username'] ?>';
		var groupName = getRadioValue('groups');

		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				document.getElementById("showMessages").innerHTML=xmlhttpInv.responseText;
			}	
		}
		xmlhttpInv.open("GET", baseUrl + "/php/displayGroupMessages.php?userName=" + userName + "&groupName=" + groupName ,true);
		xmlhttpInv.send();
	}
	
	function ajaxShowGroupUsers() {
		//var userName = '<?php echo $_SESSION['username'] ?>';
		var groupName = getRadioValue('groups');

		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				document.getElementById("showUsers").innerHTML=xmlhttpInv.responseText;
			}	
		}
		xmlhttpInv.open("GET", baseUrl + "/php/displayGroupMembers.php?groupName=" + groupName ,true);
		xmlhttpInv.send();
	}
	
	
	function ajaxSendGroupMessage() {
		var groupName = getRadioValue('groups');
		var userName = '<?php echo $_SESSION['username'] ?>';
		var message = document.getElementById('message').value;
		var retentionMode = document.getElementById('retentionMode').value;
		var expirationDate = document.getElementById('expirationDate').value;
	
		//alert('before encrypt message: ' + message);
		var encryptedMessage = encryptGroupMessage(groupName, message);
		//alert('after encrypt message: ' + encryptedMessage);
		//TODO: validate
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpGrpDel=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpGrpDel=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpGrpDel.onreadystatechange=function()
		{
			if (xmlhttpGrpDel.readyState==4 && xmlhttpGrpDel.status==200)
			{
				document.getElementById("sendMessage2").innerHTML=xmlhttpGrpDel.responseText;
			}	
		}
		xmlhttpGrpDel.open("GET","sendGroupMessage.php?groupName=" + groupName + "&userName=" + userName + "&message=" + encryptedMessage+ "&retentionMode=" + retentionMode+ "&expirationDate=" + expirationDate ,true);
		xmlhttpGrpDel.send();
	}
	
	function cancelInvite() {
		if (document.getElementById("cancelinviteID").value == "") {
			document.getElementById("invitecancelerr").innerHTML = "Please choose an invite to delete.";
		} else {
			document.getElementById("invitecancelerr").innerHTML = ""
			ajaxCancelInviteID();
		}
	}
	
	function acceptInvite() {
		if (document.getElementById("myinv").value == "") {
			document.getElementById("myinviteserr").innerHTML = "Please choose an invite to accept.";
		} else {
			document.getElementById("myinviteserr").innerHTML = "";
			ajaxAcceptInviteID();
		}
	}
	
	function declineInvite() {
		if (document.getElementById("myinv").value == "") {
			document.getElementById("myinviteserr").innerHTML = "Please choose an invite to decline.";
		} else {
			document.getElementById("myinviteserr").innerHTML = "";
			ajaxDeclineInviteID();
		}
	}

	function showDeleteGroup() {
		hideSideBarItems();
		document.getElementById("deleteGroupDiv").style.display='block';
		
		//if (document.getElementById("deleteGroup").innerHTML == "") {
		//	document.getElementById("deleteGroup2").innerHTML = ""
			ajaxShowDeleteGroup();
		//} else {
		//	document.getElementById("deleteGroup2").innerHTML = "";
		//	document.getElementById("deleteGroup").innerHTML = "";
		//}
	}

	
	function deleteGroup() {
		hideSideBarItems();
		document.getElementById("deleteGroupDiv").style.display='block';
		
		var groupName = getRadioValue('groups');
		var userName = '<?php echo $_SESSION['username'] ?>';
		
		if (groupName != "") {
			ajaxDeleteGroup();
		} else {
			document.getElementById("deleteGroup2").innerHTML = "Please select a group you'd like to delete.";
		}
	}
	
	function leaveGroup() {
		if (document.getElementById("leaveGroup").value != "") {
			ajaxLeaveGroup();
		} else {
			document.getElementById("leaveGroup").innerHTML = "Please select a group you'd like to delete.";
		}
	}
	
	function showInviteUser() {
		hideSideBarItems();
		document.getElementById("inviteUserDiv").style.display='block';
		
		//if (document.getElementById("inviteUserForm").innerHTML == "") {
			ajaxShowInviteUserForm();
		//} else {
		//	document.getElementById("inviteUserForm").innerHTML = "";
		//}
	}
	
	function displaySelectedMessage() {
		
		var rowNumber = getRadioValue('rowNumber');
		var message = document.getElementById('groupMessagesTable').rows[rowNumber].cells[4].innerHTML;
		
		var groupName = getRadioValue('groups');
		var decryptedMessage = decryptGroupMessage(groupName, message);
		alert(decryptedMessage);
	}
	
	function ajaxDeleteSelectedMessage() {
		var rowNumber = getRadioValue('rowNumber');
		var messageId = document.getElementById('groupMessagesTable').rows[rowNumber].cells[1].innerHTML;

		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject('Microsoft.XMLHTTP');
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				document.getElementById('showMessages2').innerHTML=xmlhttpInv.responseText;
			}	
		}
		xmlhttpInv.open('GET', baseUrl + '/php/deleteGroupMessage.php?messageId=' + messageId ,true);
		xmlhttpInv.send();
	}
		
	function getRadioValue(idOrName) {
		var radios = document.getElementsByTagName('input');
		for (var i=0; i<radios.length; i++) {
			var input = radios[ i ];    
			if (input.type == 'radio' && input.name == idOrName && input.checked) { 
				
				value = input.value;
				break;
			}
		}
		return value;
	}


	// AJAX Functions
	xmlhttp=new XMLHttpRequest();
	
	function ajaxCreateGroup() {
		var groupName = document.getElementById('groupName').value;
		var groupPublicKey = document.getElementById('groupPublicKey').value;
		var groupPrivateKey = document.getElementById('groupPrivateKey').value;
		var groupKeyModulus = document.getElementById('groupKeyModulus').value;
		var groupDescription = document.getElementById('groupDescription').value;
		var encryptedGroupPrivateKey = encrypt(groupPrivateKey);
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				document.getElementById("newGroup2").innerHTML=xmlhttp.responseText;
				initialLoad();
			}	
		}
		xmlhttp.open("GET","createGroup.php?groupName=" +groupName+ "&groupPublicKey=" +groupPublicKey+ "&encryptedGroupPrivateKey=" +encryptedGroupPrivateKey+ "&groupKeyModulus=" +groupKeyModulus+ "&groupDescription=" +groupDescription,true);
		xmlhttp.send();
	}
		
	function ajaxShowDeleteGroup() {
		
		var userName = $_SESSION['username'];
		var groupName = getRadioValue('groups');
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpGrpDel=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpGrpDel=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpGrpDel.onreadystatechange=function()
		{
			if (xmlhttpGrpDel.readyState==4 && xmlhttpGrpDel.status==200)
			{
				document.getElementById("deleteGroup").innerHTML=xmlhttpGrpDel.responseText;
			}	
		}
		xmlhttpGrpDel.open("GET", baseUrl + "/php/results/returndelgroupsforuser.php?userName=" + userName + "&groupName=" + groupName ,true);
		xmlhttpGrpDel.send();
	}
	
	
	
	function ajaxShowGroupMembership() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpGrp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpGrp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpGrp.onreadystatechange=function()
		{
			if (xmlhttpGrp.readyState==4 && xmlhttpGrp.status==200)
			{
				document.getElementById("groupMembership").innerHTML=xmlhttpGrp.responseText;
				alert("hi");
					for( i = 0; i < document.frmgroup.groups.length; i++ ) {
					if( document.frmgroup.groups[i].checked == true ) {
						document.getElementById("groupSelected").value =  document.frmgroup.groups[i].value;
					}
				}
			}	
		}
		xmlhttpGrp.open("GET", baseUrl + "/php/results/returngroupsforuser.php?username=" + username,true);
		xmlhttpGrp.send();
	}
	
	
	function ajaxShowOtherMembers() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpOthMem=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpOthMem=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpOthMem.onreadystatechange=function()
		{
			if (xmlhttpOthMem.readyState==4 && xmlhttpOthMem.status==200)
			{
				document.getElementById("otherMembers").innerHTML=xmlhttpOthMem.responseText;
			}	
		}
		xmlhttpOthMem.open("GET", baseUrl + "/php/results/returnothermembers.php?username=" + username,true);
		xmlhttpOthMem.send();
	}
	
	
	function ajaxShowInvites() {
		var username = document.getElementById('username').value;
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				document.getElementById("invites").innerHTML=xmlhttpInv.responseText;
			}	
		}
		xmlhttpInv.open("GET", baseUrl + "/php/results/returninvitesforgroup.php?username=" + username,true);
		xmlhttpInv.send();
	}
	
	function ajaxShowInviteUserForm() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInvForm=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInvForm=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInvForm.onreadystatechange=function()
		{
			if (xmlhttpInvForm.readyState==4 && xmlhttpInvForm.status==200)
			{
				document.getElementById("inviteUserForm").innerHTML=xmlhttpInvForm.responseText;
			}	
		}
		xmlhttpInvForm.open("GET", baseUrl + "/php/inviteuserform.php?username=" + username,true);
		xmlhttpInvForm.send();
	}
	
	
	function ajaxShowMessages() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpMsg=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpMsg=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpMsg.onreadystatechange=function()
		{
			if (xmlhttpMsg.readyState==4 && xmlhttpMsg.status==200)
			{
				document.getElementById("showMessages").innerHTML=xmlhttpMsg.responseText;
			}	
		}
		xmlhttpMsg.open("GET", baseUrl + "/php/results/returnmsgforuser.php?username=" + username,true);
		xmlhttpMsg.send();
	}
	
	
	function ajaxDeleteGroup() {
		var groupName = getRadioValue('groups');
		//var userName = '<?php echo $_SESSION['username'] ?>';
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpDel=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpDel=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpDel.onreadystatechange=function()
		{
			if (xmlhttpDel.readyState==4 && xmlhttpDel.status==200)
			{
				document.getElementById("deleteGroup").innerHTML="";
				document.getElementById("deleteGroup2").innerHTML=xmlhttpDel.responseText;
				initialLoad();
			}	
		}
		xmlhttpDel.open("GET", baseUrl + "/php/results/cascadedeletegroup.php?groupName=" + groupName,true);
		xmlhttpDel.send();
	}
	
	function ajaxLeaveGroup() {
		var username = document.getElementById('username').value;
		var groupname = document.getElementById('groupSelected').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpLeave=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpLeave=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpLeave.onreadystatechange=function()
		{
			if (xmlhttpLeave.readyState==4 && xmlhttpLeave.status==200)
			{
				document.getElementById("leaveGroup").innerHTML=xmlhttpLeave.responseText;
				initialLoad();
			}	
		}
		xmlhttpLeave.open("GET", baseUrl + "/php/results/returnleavegroup.php?username=" + username + "&groupname=" + groupname,true);
		xmlhttpLeave.send();
	}
	
	
	function ajaxInviteUser() {

		var inviteUser = document.getElementById('inviteUser').value;
		//alert('inviteUser=' + inviteUser );

		var selinviteGroup = document.getElementById('selinviteGroup').value;
		//alert('selinviteGroup=' + selinviteGroup );
		
		var groupPrivateKey = (groupKeys[selinviteGroup]).privateKey;
		//alert('groupPrivateKey=' + groupPrivateKey );
		
		var encryptedGroupPrivateKey = encryptInviteMessage ( inviteUser, groupPrivateKey );
		//alert('encryptedGroupPrivateKey=' + encryptedGroupPrivateKey );
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInvActn=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInvActn=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInvActn.onreadystatechange=function()
		{
			if (xmlhttpInvActn.readyState==4 && xmlhttpInvActn.status==200)
			{
				document.getElementById("inviteUserForm").innerHTML=xmlhttpInvActn.responseText;
				ajaxShowInvites();
			}	
		}
		xmlhttpInvActn.open("GET", baseUrl + "/php/results/returninviteuser.php?username=" + userName + "&inviteUser=" + inviteUser + "&selinviteGroup=" + selinviteGroup + 
		"&encGroupPrivKey=" + encryptedGroupPrivateKey,true);
		xmlhttpInvActn.send();
	}
	
	
	function ajaxCancelInviteID() {
		var cancelinviteID = document.getElementById('cancelinviteID').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInvCancel=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInvCancel=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInvCancel.onreadystatechange=function()
		{
			if (xmlhttpInvCancel.readyState==4 && xmlhttpInvCancel.status==200)
			{
				document.getElementById("invitecancelerr").innerHTML=xmlhttpInvCancel.responseText;
				ajaxShowInvites();
			}	
		}
		xmlhttpInvCancel.open("GET", baseUrl + "/php/results/returncancelinvite.php?cancelinviteID=" + cancelinviteID,true);
		xmlhttpInvCancel.send();
	}
	
	function ajaxShowMyInvites() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpMyInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpMyInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpMyInv.onreadystatechange=function()
		{
			if (xmlhttpMyInv.readyState==4 && xmlhttpMyInv.status==200)
			{
				document.getElementById("myinvites").innerHTML=xmlhttpMyInv.responseText;
			}	
		}
		xmlhttpMyInv.open("GET", baseUrl + "/php/results/returnmyinvites.php?username=" + username,true);
		xmlhttpMyInv.send();
	}

	function ajaxAcceptInviteID() {
		var myinvid = document.getElementById('myinv').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpMyInvAccept=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpMyInvAccept=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpMyInvAccept.onreadystatechange=function()
		{
			if (xmlhttpMyInvAccept.readyState==4 && xmlhttpMyInvAccept.status==200)
			{
				document.getElementById("myinviteserr").innerHTML=xmlhttpMyInvAccept.responseText;
				initialLoad();
			}	
		}
		xmlhttpMyInvAccept.open("GET", baseUrl + "/php/results/acceptinvite.php?myinv=" + myinvid,true);
		xmlhttpMyInvAccept.send();
	}	
	
	
	function ajaxDeclineInviteID() {
		var myinvid = document.getElementById('myinv').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpMyInvDecline=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpMyInvDecline=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpMyInvDecline.onreadystatechange=function()
		{
			if (xmlhttpMyInvDecline.readyState==4 && xmlhttpMyInvDecline.status==200)
			{
				document.getElementById("myinviteserr").innerHTML=xmlhttpMyInvDecline.responseText;
				refresh();
			}	
		}
		xmlhttpMyInvDecline.open("GET", baseUrl + "/php/results/declineinvite.php?myinv=" + myinvid,true);
		xmlhttpMyInvDecline.send();
	}		
	
	-->
</script>

</head>

<body onload="initialLoad();">


<button onclick='loadKey();' >Load your key</button>
<button onclick='loadGroupKeys();' >Load your group keys</button>
<button onclick='showKey( key );' >Show key</button>
<button onclick='testEncryptDecrypt("Apple")' >Encrypt &quot;Apple&quot;</button>
<button onclick='testEncryptDecryptGroupPrivateKey()' > Test encrypt/decrypt group private key</button>
<button onclick='testCreateGroup()'>Test create group</button>
<button onclick='testSendMessage()'>Test send message</button>

<div id="container">
<div id="wrap">
<div id="header">
<?php

echo "<p>	User " . $_SESSION['username'] . ", welcome to your Privr account. </p>\r\n";
echo "<input type='hidden' id='username' value='" . $_SESSION['username'] . "' />";
echo "<input type='hidden' id='groupname' value='" . $_SESSION['groupname'] . "' />";
?>
</div>


<div id="nav">
<?php include("includes/navigationbar.html"); ?>
</div>
<div id="main">

<div id="privateKey">temp</div>
<!-- Group Membership -->
<p>Your Group Membership:</p>
<p class="warning">These are groups you belong to.</p>
<div id="groupMembership"></div>
<div id="leaveGroup"></div>
<!-- End Group Membership -->

<!-- Invites -->
<p>Outgoing group invites:</p>
<div id="invites"></div>
<div id="invitecancelerr"></div>
<br />
<p>Incoming group invites:</p>
<div id="myinvites"></div>
<div id="myinviteserr"></div>
<!-- End Invites -->

</div>

<div id="sidebar">

	<div id="inviteUserDiv">
		<div id="inviteUserForm"></div>
	</div>
	
	<!-- Show Message -->
	<div id="showMessagesDiv">
		<div id="showMessages"></div>
		<div id="showMessages2"></div>
	</div>
	<!-- End Show Message -->

	<!-- Send Message -->
	<div id="sendMessageDiv">
		<div id="sendMessage"></div>
		<div id="sendMessage2"></div>
	</div>
	<!-- End Send Message -->
	
	<!-- List group users -->
	<div id="showUsersDiv">
		<div id="showUsers"></div>
	</div>
	<!-- End Send Message -->


	<div id="newGroupDiv">
		<div id="newGroup"></div>
		<div id="newGroup2"></div>
	</div>
	
	<div id="deleteGroupDiv">
		<div id="deleteGroup"></div>
		<div id="deleteGroup2"></div>
	</div>
	
	<!-- Group Member List -->
	<div id="groupMembers">
		<p>Group Members</p>
		<p class="warning">Remember, members in one group cannot see members in other groups unless they are part of that group too!</p>
		<div id="otherMembers"></div>
	</div>
	<!-- End Group Member List -->

	<!-- Messages (deprecate?) -->
	<div id="showMessagesGroup2">
		<h2>Messages from your groups:</h2>
		<div id="messages"></div>
		<div id="newMessageForm"></div>
	</div>
	<!-- End Messages -->
	
	<div id="logoutDiv">
		<div id="logoutInfo"></div>
	</div>

</div> <!-- end sidebar -->

<div id="footer">
<p class="warning">Use freely - ask for code and we'll be happy to provide.  No restrictions. Live, love, be free.</p></div>
</div> <!-- end wrap -->
</div> <!-- end container -->


</body>
</html>
